/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gcalscraper.wapo;

import gcalscraper.wapo.date.DateParser;
import gcalscraper.wapo.date.Occurrence;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.Nodes;

/**
 *
 * @author dave
 */
public class Event {
  
  private String number;
  private String eventType;
  private String detailLink;
  private String name;
  private String date;
  private String shortDesc;
  private String venueLink;
  private String venue;
  private String venueLocation;
  private String detailedDateTime;

  @Override
  public String toString() {
    return name + " @ " + venue + ", " + detailedDateTime;
  }

  public String getDate() {
    return date;
  }

  public void setDate(String date) {
    this.date = date;
  }

  public String getDetailLink() {
    return detailLink;
  }

  public void setDetailLink(String detailLink) {
    this.detailLink = detailLink;
  }

  public String getDetailedDateTime() {
    return detailedDateTime;
  }
  
  public void setDetailedDateTime(String d) {
    detailedDateTime = d;
  }
  
  public void setDetailedDateTime(Node node) {
    Element element = (Element) node;
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < element.getChildCount(); i++) {
      if (i > 0)
        sb.append("\n");
      sb.append(element.getChild(i).getValue());
    }    
    
    detailedDateTime = sb.toString().replaceAll("a\\.m\\.", "AM")
        .replaceAll("p\\.m\\.", "PM")
        .replaceAll("[Nn][Oo][Oo][Nn]", "12 PM"); 
    
    /*
    sb = new StringBuffer();
    sb.append("---\nhttp://www.washingtonpost.com" + detailLink + "\n");
    sb.append(detailedDateTime + "\n");
    List<Occurrence> occurrences = new DateParser().parse(this);
    for (Occurrence o : occurrences) {
      sb.append("  " + o + "\n");
    }
    if (occurrences.size() == 0)
      System.out.println(sb.toString());*/
  }

  public String getEventType() {
    return eventType;
  }

  public void setEventType(String eventType) {
    this.eventType = eventType;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getNumber() {
    return number;
  }

  public void setNumber(String number) {
    this.number = number;
  }

  public String getShortDesc() {
    return shortDesc;
  }

  public void setShortDesc(String shortDesc) {
    this.shortDesc = shortDesc;
  }

  public String getVenue() {
    return venue;
  }

  public void setVenue(String venue) {
    this.venue = venue;
  }

  public String getVenueLink() {
    return venueLink;
  }

  public void setVenueLink(String venueLink) {
    this.venueLink = venueLink;
  }

  public String getVenueLocation() {
    return venueLocation;
  }

  public void setVenueLocation(String venueLocation) {
    this.venueLocation = venueLocation;
  }
  
  private static Pattern pattern = Pattern.compile(".*&id=([0-9]+).*");
      
  public Event() {
  }
  
  public Event(Node parentTD) {
    number = get(parentTD, ".//span[@class='res_num']/text()");
    eventType = get(parentTD, ".//span[@class='res_kicker']/b/text()");
    detailLink = get(parentTD, ".//div[@class='res_name']/a/@href");
    name = get(parentTD, ".//div[@class='res_name']/a/text()");
    date = get(parentTD, ".//td[@class='third_column']/div/text()");
    shortDesc = get(parentTD, ".//td[@class='res_copy']/text()");
    venueLink = get(parentTD, ".//td[@class='res_address_event']/a/@href");
    venue = get(parentTD, ".//td[@class='res_address_event']/a/text()");
    venueLocation = get(parentTD, ".//td[@class='res_address_event']/text()[3]");
    
    // sanitize detail link    
    Matcher m = pattern.matcher(detailLink);
    if (m.matches()) {
      detailLink = "/ac2/wp-dyn?node=cityguide/profile&id=" + m.group(1);
    }
  }
  
  private static String get(Node start, String q) {
    Nodes nodes = start.query(q);
    return nodes.get(0).getValue().trim();
  }
}
